package com.hmily.dubbo.consumer.sync;

import com.hmily.dubbo.api.UserUpdateFacade;
import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.extension.ExtensionLoader;
import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.ReferenceConfig;
import org.apache.dubbo.config.RegistryConfig;
import org.apache.dubbo.registry.Registry;
import org.apache.dubbo.registry.RegistryFactory;

import java.io.IOException;

/**
 * <h1>Zookeeper 动态注册降级策略</h1>
 *
 * @author hmilyylimh
 *         ^_^
 * @version 0.0.1
 *         ^_^
 * @date 2021-11-29
 *
 */
public class JavaCodeZkRegisterSvrConsumerApplication {

    public static void main(String[] args) throws IOException {
        // 设置应用服务名称
        ApplicationConfig applicationConfig = new ApplicationConfig("dubbo-consumer");
        // 设置注册中心的地址
        RegistryConfig registryConfig = new RegistryConfig("zookeeper://127.0.0.1:2181");

        // 创建引用 UserUpdateFacade 这个服务的对象
        ReferenceConfig<UserUpdateFacade> referenceConfig = new ReferenceConfig<>();
        referenceConfig.setApplication(applicationConfig);
        referenceConfig.setRegistry(registryConfig);
        referenceConfig.setInterface(UserUpdateFacade.class);

        // 直接拿到引用的代理对象，然后进行远程调用
        UserUpdateFacade userUpdateFacade = referenceConfig.get();
        System.out.println(userUpdateFacade.updateUserById("G314", "yuki"));

        startSubThread();

        // 阻塞等待
        System.in.read();
    }

    private static void startSubThread() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                // 获取服务注册中心工厂对象
                RegistryFactory registryFactory =
                        ExtensionLoader.getExtensionLoader(RegistryFactory.class).getAdaptiveExtension();
                Registry registry = registryFactory.getRegistry(URL.valueOf("zookeeper://127.0.0.1:2181"));

                // 直接进行注销动作，即 Override 操作，开启降级操作
                String mockVal = "force: return null";
                registry.register(URL.valueOf("override://0.0.0.0/com.hmily.dubbo.api.UserUpdateFacade?mock=" +
                        mockVal
                        + "&application=dubbo-consumer&dubbo=2.0.2&interface=com.hmily.dubbo.api.UserUpdateFacade"));
                sleepMills(2000);

                // 再次检测一下，是否还能调用通？
                ReferenceConfig<UserUpdateFacade> referenceConfig = new ReferenceConfig<>();
                referenceConfig.setApplication(new ApplicationConfig("dubbo-consumer"));
                referenceConfig.setRegistry(new RegistryConfig("zookeeper://127.0.0.1:2181"));
                referenceConfig.setInterface(UserUpdateFacade.class);
                System.out.println("降级后的结果：" + referenceConfig.get().updateUserById("GGGGGG314", "yyyyyyyuki"));

                // 取消降级
                registry.unregister(URL.valueOf("override://0.0.0.0/com.hmily.dubbo.api.UserUpdateFacade?mock=" +
                        mockVal
                        + "&application=dubbo-consumer&dubbo=2.0.2&interface=com.hmily.dubbo.api.UserUpdateFacade"));
                sleepMills(2000);
                ReferenceConfig<UserUpdateFacade> referenceConfig1 = new ReferenceConfig<>();
                referenceConfig1.setApplication(new ApplicationConfig("dubbo-consumer"));
                referenceConfig1.setRegistry(new RegistryConfig("zookeeper://127.0.0.1:2181"));
                referenceConfig1.setInterface(UserUpdateFacade.class);
                System.out.println("取消降级后的结果：" + referenceConfig1.get().updateUserById("XXXXXG314", "xxxxxyyuki"));

            }
        }).start();
    }

    private static void sleepMills(int i) {
        try {
            Thread.sleep(i);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
